package com.gph.saviorframework.fastdfs.proto.handler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;

import com.gph.saviorframework.fastdfs.proto.BytesUtil;
import com.gph.saviorframework.fastdfs.proto.CmdConstants;
import com.gph.saviorframework.fastdfs.proto.OtherConstants;

public class StorageTruncateHandler extends AbstractHandler<Void> {

	private static final byte cmd = CmdConstants.STORAGE_PROTO_CMD_TRUNCATE_FILE;

	private final long size;
	private final String path;
	private final Charset charset;

	/**
	 * @param ins
	 * @param size
	 * @param ext
	 */
	public StorageTruncateHandler(Socket socket,  
			String path, long size , Charset charset) {
		super(socket);
		this.size = size;
		this.charset = charset;
		this.path = path;
	}

	@Override
	protected void send(OutputStream ous) throws IOException {

		byte[] header;
		byte[] hexLenBytes;
		int offset;
		long body_len;

		byte[] pathBytes = path.getBytes(charset);
		body_len = 2 * OtherConstants.FDFS_PROTO_PKG_LEN_SIZE
				+ pathBytes.length;

		header = packHeader(cmd, body_len);
		
		byte[] wholePkg = new byte[(int) (header.length + body_len)];
		System.arraycopy(header, 0, wholePkg, 0, header.length);
		offset = header.length;

		hexLenBytes = BytesUtil.long2buff(path.length());
		System.arraycopy(hexLenBytes, 0, wholePkg, offset,
				hexLenBytes.length);
		offset += hexLenBytes.length;

		hexLenBytes = BytesUtil.long2buff(size);
		System.arraycopy(hexLenBytes, 0, wholePkg, offset,
				hexLenBytes.length);
		offset += hexLenBytes.length;

		

		System.arraycopy(pathBytes, 0, wholePkg, offset,
				pathBytes.length);

		ous.write(wholePkg);

	}

	@Override
	protected void receive(InputStream ins) throws IOException {

		receiveHeader(ins);

		if (contentLength != 0) {
			throw new IOException("读取到的数据长度与协议长度不符");
		}

	}

}
